uniform sampler2D frame;
uniform sampler2D map;

uniform int BW, viewMap;
uniform vec2 npix;

void main()
{
	vec2 tc = gl_TexCoord[0].st;
	vec4 color;
	if(viewMap == 1)
		color = texture2D(map,tc);
	else
	{
		color = texture2D(frame,tc);
		float c;
		vec2 nims;
		if(BW == 1)
		{
			c = floor((color.r + color.g + color.b)/3.*255.);
			nims = vec2(16., 16.);
		}
		else
		{
			float r,g,b,bx,by;
			r = 255.*color.r; b = 255.*color.b; g = 255.*color.g;
	        r = floor(r/16.); g = floor(g/16.); b = floor(b/16.);
	        bx = mod(b,4.); by = floor(b/4.);
	        c = r + bx*16. + g*64. + by*1024.;
			nims = vec2(64., 64.);
		}	
		float y = nims[0] - floor(c/nims[0]) - 1.;
		float x = floor(mod(c,nims[0]));
		
		color = texture2D(map, mod(tc, 1./npix)*npix/nims + vec2(x,y)/nims);
	}
	gl_FragColor = color;
	gl_FragColor.a = 1.0;

}
